#include "tim4.h"


void tim4_init(void)
{
    //1.时钟PB6
    RCC->APB2ENR|=RCC_APB2ENR_IOPBEN;
    RCC->APB1ENR|=RCC_APB1ENR_TIM4EN;
    //2.配置    浮空输入 mode 00  CNF 01
    GPIOA->CRL&=~GPIO_CRL_MODE6;
    GPIOA->CRL|=GPIO_CRL_CNF6_0;
    GPIOA->CRL&=~GPIO_CRL_CNF6_1;

    //3.配置定时器上升与arr这些
    TIM4->CR1&=~TIM_CR1_DIR;
    TIM4->ARR=65535;
    TIM4->PSC=71;

    //.4配置一些01
    TIM4->CR2 &= ~TIM_CR2_TI1S;
    //TIM4->CCMR1 &= ~TIM_CCMR1_IC1F;
    TIM4->CCMR1 |=TIM_CCMR1_CC1S_0;
    TIM4->CCMR1 &=~TIM_CCMR1_CC1S_1;
    TIM4->CCER &= ~TIM_CCER_CC1P;
    //
    //TIM4->CCMR1 &= ~TIM_CCMR1_IC1PSC;
    TIM4->CCER|=TIM_CCER_CC1E;
    TIM4->DIER|=TIM_DIER_CC1IE;
    
    NVIC_SetPriorityGrouping(3);
    NVIC_SetPriority(TIM4_IRQn,3);
    NVIC_EnableIRQ(TIM4_IRQn);



}

void tim4_start(void)
{
    TIM4->CR1|=TIM_CR1_CEN;
}
void tim4_stop(void)
{
    TIM4->CR1&=~TIM_CR1_CEN;
}

uint8_t cnt=0;
uint16_t value;

void TIM4_IRQHandler(void)
{
   if(TIM4->SR & TIM_SR_CC1IF)
   {
        cnt++;
        TIM4->SR &= ~TIM_SR_CC1IF;
        if(cnt==1)
        {
            TIM4->CNT=0;
        }
        else if(cnt==2)
        {
            value=TIM4->CCR1;
            cnt=0;
        }
   }
}
